package com.ljl.ssm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljl.ssm.entity.Category;
import com.ljl.ssm.entity.Product;
import com.ljl.ssm.mapper.CategoryMapper;
import com.ljl.ssm.mapper.ProductMapper;
import com.ljl.ssm.pojo.ProductExample;
import com.ljl.ssm.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author BaoZhou
 * @since 2019-07-07
 */
@SuppressWarnings("ALL")
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public PageInfo selectList(int pn, String keyword) {
        List<Product> productList=new ArrayList<>();
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",keyword);
        Category category=categoryMapper.selectOne(queryWrapper);
        QueryWrapper<Product>  queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("category_id",category.getId());
        PageHelper.startPage(pn,5);
        productList=productMapper.selectList(queryWrapper2);
        PageInfo pageInfo = new PageInfo<>(productList);
        return pageInfo;
    }


    @Override
    public PageInfo selectListbySize(int size, int keyword) {

        List<Product> productList=new ArrayList<>();
        QueryWrapper<Product>  queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category_id",keyword);
        PageHelper.startPage(1,size);
        productList=productMapper.selectList(queryWrapper);
        PageInfo pageInfo = new PageInfo<>(productList);

        return pageInfo;
    }

    @Override
    public PageInfo selectListLike(int pn,String keyword) {
        List<Product> productList=new ArrayList<>();
        QueryWrapper<Product>  queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name",keyword);
        PageHelper.startPage(1,5);
        productList=productMapper.selectList(queryWrapper);
        PageInfo pageInfo = new PageInfo<>(productList);
        return pageInfo;
    }
    @Override
    public void add(Product product) {
        productMapper.insert(product);
    }

    @Override
    public PageInfo selectAll() {
        List<Category> productList=new ArrayList<>();
        PageHelper.startPage(1,5);
        productList=categoryMapper.selectList(null);
        PageInfo pageInfo = new PageInfo<>(productList);
        return pageInfo;
    }

    @Override
    public void delete(Integer id) {
        productMapper.deleteById(id);
    }

    @Override
    public void update(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public Product get(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public Product getbyId(Integer id) {
        Product product=productMapper.selectById(id);
        return product;
    }

    @Override
    public List<Product> list(Integer category_id) {
        ProductExample example = new ProductExample();
        example.or().andCategory_idEqualTo(category_id);
        List<Product> products = productMapper.selectByExample(example);
        return products;
    }

    @Override
    public void fill(List<Category> categories) {

    }

    @Override
    public void fill(Category category) {

    }

    @Override
    public void fillByRow(List<Category> categories) {

    }

    @Override
    public void setReviewCount(Product product) {
//        int reviewCount = reviewService.getCount(product.getId());
//        product.setReviewCount(reviewCount);
    }

    @Override
    public List<Product> search(String keyword) {
        ProductExample example = new ProductExample();
        example.or().andNameLike("%" + keyword + "%");
        example.setOrderByClause("id desc");
        return productMapper.selectByExample(example);

    }

    @Override
    public List<Product> selectList(String keyword) {
        List<Product> productList=new ArrayList<>();
        QueryWrapper<Product>  queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name",keyword);
        productList=productMapper.selectList(queryWrapper);
        return productList;
    }
}
